<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 * 
 * @version $Id: UserIdentity.php,v 1.3 2011-06-17 04:43:20 herve Exp $
 * @copyright 2011
 */
class UserIdentity extends CUserIdentity {

	private $_user;

	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate() {
		$record = User::model()->findByAttributes(array (
			'username' => $this->username
		));
		if ($record == null) {
			if (defined('YII_DEBUG') && YII_DEBUG) {
				$this->errorCode = self::ERROR_USERNAME_INVALID;
			} else {
				$this->errorCode = self::ERROR_UNKNOWN_IDENTITY;
			}
		} else {
			if ($record->password !== md5($this->password)) {
				if (defined('YII_DEBUG') && YII_DEBUG) {
					$this->errorCode = self::ERROR_PASSWORD_INVALID;
				} else {
					$this->errorCode = self::ERROR_UNKNOWN_IDENTITY;
				}
			} else {
				$this->_user = $record;
				$this->setState('dbuser', $this->_user);
				$this->errorCode = self::ERROR_NONE;
			}
		}
		return !$this->errorCode;
	}

	public function getUser() {
		return $this->_user;
	}

}
?>
